/**
 * json配置举例，更多配置请看 IConfigSchema
 * @author luguoxiang
 * @date 2022/7/5
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
import { FormRule } from 'tdesign-vue-next';
import {
  FilterTypeEnum,
  has,
  IConfigSchema,
  IFormSchema, ITableSchema,
  LgBizSelect,
  LgBizSelectProps,
  LgBizTree,
  LgBizTreeProps,
  LgDictProps,
} from '@/soar';

const fieldList: IConfigSchema<any>[] = [
  {
    field: 'avatar',
    label: '头像',
    dataType: 'img',
    params: 'url',
    search: false,
    table: {
      width: 60,
    },
  },
  {
    field: 'roles',
    label: '角色',
    table: false,
    search: false,
    form: false,
    detail: {
      slots: 'roles',
      colProps: {
        style: '--lg-grid-min: 2; --lg-grid-max: 9',
      },
    },
  },
  {
    field: 'nickname',
    label: '昵称',
    dataType: 'string',
    form: {
      rules: true, // true方式配置校验，必填
    },
  },
  {
    field: 'username',
    label: '账号',
    dataType: 'string',
    table: {
      sorter: true,
    },
    form: {
      disabled: ({ isUpdate }) => isUpdate,
      rules: true, // true方式配置校验，必填
    },
  },
  {
    field: 'userType',
    label: '用户类型',
    dataType: 'dict',
    params: 'user-type',
    table: {
      width: 120,
      sorter: true,
    },
    form: false,
  },
  {
    field: 'lastLoginDate',
    label: '最后登录时间',
    dataType: 'datetime',
    table: {
      width: 160,
    },
    form: false,
  },
  {
    field: 'loginCount',
    label: '登录次数(成功)',
    dataType: 'number',
    table: {
      width: 120,
    },
    form: false,
  },
  /*  {
    field: 'tenantId',
    label: '租户',
    dataType: 'string',
    table: {
      colKey: 'tenantIdName',
    },
    search: false,
    detail: {
      field: 'tenantIdName',
    },
    form: false,
  }, */
  {
    field: 'status',
    label: '状态',
    dataType: 'dict',
    params: 'user-status', // 字符串配置选项，值是索引
    table: {
      width: 100,
      sorter: true,
    },
    search: {
      componentProps: {
        component: 't-radio-group',
      } as LgDictProps,
    },
    form: {
      componentProps: {
        component: 't-radio-group',
      } as LgDictProps,
    },
  },
  /*  {
    field: 'orgIds',
    label: '所属机构',
    dataType: 'string',
    search: {
      component: LgBizTree,
      componentProps: {
        placeholder: '请选择${label}',
        data: '/system/org/query',
        clearable: true,
      } as LgBizTreeProps,
      searchType: FilterTypeEnum.CUSTOM,
    },
    table: false,
    form: false,
    detail: false,
  }, */
  {
    field: 'roleIds',
    label: '所属角色',
    dataType: 'string',
    search: {
      component: LgBizTree,
      componentProps: {
        placeholder: '请选择${label}',
        data: '/system/role/query',
        clearable: true,
      } as LgBizTreeProps,
      searchType: FilterTypeEnum.CUSTOM,
    },
    table: false,
    form: false,
    detail: false,
  },
  {
    field: 'tenantIds',
    label: '所属租户',
    dataType: 'string',
    predicate(): boolean {
      return has.admin();
    },
    search: {
      component: LgBizSelect,
      componentProps: {
        placeholder: '请选择${label}',
        data: '/system/tenant/query',
        clearable: true,
      } as LgBizSelectProps,
      searchType: FilterTypeEnum.CUSTOM,
    },
    table: false,
    form: false,
    detail: false,
  },
];

/**
 * 修改密码表单
 */
export const updatePasswordForm: IFormSchema<{ password; cPassword }>[] = [
  {
    field: 'password',
    label: '密码',
    dataType: 'string',
    componentProps: {
      type: 'password',
    },
    rules: true,
  },
  {
    field: 'cPassword',
    label: '确认密码',
    dataType: 'string',
    componentProps: {
      type: 'password',
    },
    rules({ data }) {
      return [
        { required: true, message: '请再次输入密码' },
        { message: '两次密码不一致', validator: (value) => value === data.password },
      ] as FormRule[];
    },
  },
];

export const orgSchema: ITableSchema<any>[] = [
  {
    field: 'main',
    label: '主要的',
    dataType: 'boolean',
    width: 80,
  },
  {
    field: 'orgId',
    label: '组织',
    dataType: 'string',
    slots: 'orgId',
  },
  {
    field: 'postId',
    label: '职位',
    dataType: 'string',
    slots: 'postId',
    width: 200,
  },
];

/**
 *
 */
export const accountTypeSchema: IConfigSchema<any>[] = [
  {
    field: 'accountType',
    label: '类型',
    dataType: 'dict',
    params: 'account-bind-type',
    table: {
      width: 80,
    },
  },
  {
    field: 'account',
    label: '账号',
  },
  {
    field: 'createTime',
    label: '绑定时间',
    table: {
      width: 160,
    },
  },
  {
    field: 'lastLoginDate',
    label: '最后登录时间',
    table: {
      width: 160,
    },
  },
  {
    field: 'loginCount',
    label: '登录次数(成功)',
    table: {
      width: 120,
    },
  },
  {
    field: 'remark',
    label: '备注',
  },
];

export default fieldList;
